package com.hot100;

import java.util.*;

public class Solution207 {
    public static void main(String[] args) {
        int numCourses = 2;
        int[][] prerequisites = new int[1][2];
        prerequisites[0][0] = 1;
        prerequisites[0][1] = 0;
        canFinish(numCourses,prerequisites);
    }
    public static boolean canFinish(int numCourses, int[][] prerequisites) {
        int[] rudu = new int[numCourses];
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < prerequisites.length; i++) {
            rudu[prerequisites[i][0]]++;
            List<Integer> list = map.getOrDefault(prerequisites[i][1], new ArrayList<>());
            list.add(prerequisites[i][0]);
            map.put(prerequisites[i][1], list);
        }
        List<Integer> deque = new ArrayList<>();
        for (int i = 0; i < rudu.length; i++) {
            if (rudu[i] == 0) deque.add(i);
        }
        while (!deque.isEmpty()) {
            int tmp = deque.remove(0);
            numCourses--;
            List<Integer> integerList = map.getOrDefault(tmp,new ArrayList<>());
            for (Integer integer : integerList) {
                rudu[integer]--;
                if (rudu[integer] == 0) deque.add(integer);
            }
        }
        return numCourses == 0;
    }
}
